package com.rst.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.ClassTiming;
import com.rst.model.Section;
import com.rst.model.TimeTable;
import com.rst.model.Weekday;
import com.rst.service.ClassService;
import com.rst.service.SectionService;
import com.rst.service.TimeTableService;


@Controller
public class TimeTableController {
	
	@Autowired
	SectionService sectionService;
	
	@Autowired
	TimeTableService timeTableService;
	
	@Autowired
	ClassService classService;

	@RequestMapping(value="/addWeekday", method=RequestMethod.GET)
	public ModelAndView addWeekday()
	{
		ModelAndView mav = new ModelAndView("timetable/weekday/createWeekday");
		Weekday weekday = new Weekday();
		List<Section> sectionList = sectionService.getSectionList();;
		mav.addObject("sectionList", sectionList);
		mav.getModelMap().put("weekday", weekday);
		return mav;
	}
	
	@RequestMapping(value="/saveWeekday", method=RequestMethod.POST)
	public String saveSection(@ModelAttribute("weekday")Weekday weekday, BindingResult result, SessionStatus status)
	{
		Long id = timeTableService.save(weekday);
		status.setComplete();
		return "redirect:manageweekday.htm";
	}
	
	@RequestMapping(value="/editWeekday", method=RequestMethod.GET)
	public ModelAndView editWeekday(@RequestParam("id")Long weekdayId)
	{
		ModelAndView mav = new ModelAndView("section/editSection");
		Section section = sectionService.getSectionById(weekdayId);
		List<Section> sectionList = sectionService.getSectionList();;
		mav.addObject("sectionList", sectionList);
		mav.addObject("editSection", section);
		return mav;
	}
	

	@RequestMapping(value="/classtiming", method=RequestMethod.GET)
	public ModelAndView classTiming()
	{
		ModelAndView mav = new ModelAndView("timetable/classtiming/classTiming");		
		List<ClassTiming> classTimingList = timeTableService.getclassTimingList();
		mav.addObject("classTimingList", classTimingList);
		return mav;
	}

	@RequestMapping(value="/addclasstiming", method=RequestMethod.GET)
	public ModelAndView addclasstiming()
	{
		ModelAndView mav = new ModelAndView("timetable/classtiming/addClasstiming");
		ClassTiming classTiming = new ClassTiming();
		List<Section> sectionList = sectionService.getSectionList();;
		mav.addObject("sectionList", sectionList);
		mav.getModelMap().put("classTiming", classTiming);
		return mav;
	}
	
	@RequestMapping(value="/saveClasstiming", method=RequestMethod.POST)
	public String saveClasstiming(@ModelAttribute("classTiming")ClassTiming classTiming, BindingResult result, SessionStatus status)
	{
		Long id = timeTableService.saveClassTiming(classTiming);
		status.setComplete();
		return "redirect:manageweekday.htm";
	}
	
	@RequestMapping(value="/editclasstiming", method=RequestMethod.GET)
	public ModelAndView editclasstiming(@RequestParam("id")Long classTimingId)
	{
		ModelAndView mav = new ModelAndView("timetable/classtiming/editClasstiming");
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		ClassTiming classTiming = timeTableService.getClasstimingById(classTimingId);
		mav.addObject("classTiming", classTiming);
		return mav;
	}
	
	@RequestMapping(value="/createtimetable", method=RequestMethod.GET)
	public ModelAndView createTimeTable()
	{
		ModelAndView mav = new ModelAndView("timetable/createTimeTable");
		TimeTable timeTable = new TimeTable();
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		mav.getModelMap().put("timeTable", timeTable);
		return mav;
	}
	
	@RequestMapping(value="/saveTimeTable", method=RequestMethod.POST)
	public String saveTimeTable(@ModelAttribute("timeTable")TimeTable timeTable, BindingResult result, SessionStatus status)
	{
		Long id = timeTableService.saveTimeTable(timeTable);
		status.setComplete();
		return "redirect:manageweekday.htm";
	}

	@RequestMapping(value="/ajax/getClassTimeTableAjax", method=RequestMethod.GET)
	public @ResponseBody String getClassTimeTableAjax(@RequestParam("id")Long sectionId)
	{
		
		List<ClassTiming> classTimingList = timeTableService.getClasstimingBySectionId(sectionId);
		Weekday weekday = timeTableService.getClassWeekdaysBySectionId(sectionId);
		return "";
	}
}
